import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * &#064;date  2022/8/15
 */
public class NGuessNumber {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String input;
        while (true) {
            int[] answer = generateAnswer();
            while (true) {
                boolean a = isWin(compareGuessAnswer(answer, getPlayerInput()));
                if (a) {
                    System.out.println("你猜对了");
                    break;
                } else {
                    System.out.println("猜错了，再猜一次吧");
                }
            }
            System.out.println("继续游戏请输入y,退出游戏输入n");
            while (true){
                input = scanner.next();
                if ("y".equals(input)|| "n".equals(input)){
                    break;
                }else {
                    System.out.println("输入有误");
                }
            }
            if ("y".equals(input)) {
                continue;
            } else {
                break;
            }
        }
    }
        public static int[] generateAnswer() {
            Random random = new Random();
            int[] guess = new int[4];
            int ran = random.nextInt(10);
            guess[0] = ran;
            for (int i = 0; i < guess.length; i++) {
                ran = random.nextInt(10);
                for (int num = 0; num < i; num++) {
                    while (ran == guess[num]) {
                        ran = random.nextInt(10);
                        num = 0;
                    }
                }
                guess[i] = ran;
            }
            for (int i = 0; i < guess.length; i++) {
                for (int j = i + 1; j < guess.length; j++) {
                    if (guess[i] == guess[j]) {
                        guess[j] = (guess[j] * guess[j] + 1) % 10;
                    }
                }
            }
            System.out.println(Arrays.toString(guess));
            return guess;
        }
    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int[] number = new int[4];
        int t = 1;
        System.out.println("请输入四个互不重复的数");
        for (int i = 0; i < number.length; i++) {
            System.out.println("第" + (i + 1) + "个");
            number[i] = scanner.nextInt();
        }
        for (int i = 0; i < number.length; i++) {
            for (int j = 0; j < number.length; j++) {
                if ((number[i] == number[j]) && (j != i)) {
                    t = 1;
                    break;
                } else {
                    t = 0;
                }
            }
            if (t == 1) {
                break;
            }
        }
        if (t == 1) {
            System.out.println("输入重复，请重新输入");
        }
        return number;
    }
    public static int compareGuessAnswer(int[] guess, int[] number) {
        int a = 0, b = 0;
        for (int i = 0; i < guess.length; i++) {
            for (int k : number) {
                if (guess[i] == number[i]) {
                    a++;
                    break;
                }
                if (k == guess[i]) {
                    b++;
                }
            }
        }
        System.out.printf("%dA%dB \n",a,b);
        return a;
    }
    public static boolean isWin(int a) {
        boolean boo;
        boo = a == 4;
        return boo;
        }
    }
